/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.adapter.CarFixAdapter;
import com.gmail.blackdog1987.carcare.entity.TCarfixshop;
import com.gmail.blackdog1987.carcare.entity.base.DataPage;
import com.gmail.blackdog1987.carcare.network.request.builder.CarFixShopRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.CarFixShopResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.CarFixShopResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class CarFixActivity extends BaseActivity implements OnResponseListener,
		OnClickListener, OnLastItemVisibleListener,
		OnRefreshListener2<ListView>, OnItemClickListener {
	private Context mContext;
	private PullToRefreshListView mListView;
	private ListView mActualListView;
	private TextView mDistance;
	private TextView mPrice;
	private CarFixAdapter mAdapter;
	
	private CarFixShopRequestBuilder mReq = new CarFixShopRequestBuilder();
	private CarFixShopResponseHandler mHandler = new CarFixShopResponseHandler();
	
	private DataPage<TCarfixshop> page;
	private String order = "1";
	Intent i;
	
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_carfix);
		mContext = this;
		i = new Intent(CarFixActivity.this, OrderCarFixActivity.class);
		initViews();
		reg();
		initData(savedInstanceState);
	}
	
	public void initViews() {
		mDistance = (TextView) findViewById(R.id.carfix_distance);
		mPrice = (TextView) findViewById(R.id.carfix_price);
		mListView = (PullToRefreshListView) findViewById(R.id.carfix_list);
		mListView.setMode(Mode.BOTH);
		mActualListView = mListView.getRefreshableView();
	}
	
	public void reg() {
		mListView.setOnRefreshListener(this);
		mListView.setOnLastItemVisibleListener(this);
		mListView.setOnItemClickListener(this);
		mHandler.setOnResponseListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		mDistance.setOnClickListener(this);
		mPrice.setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		mAdapter = new CarFixAdapter(mContext, null);
		mListView.setAdapter(mAdapter);
		mReq = new CarFixShopRequestBuilder();
		mReq.setLat(0.0)
			.setLon(0.0)
			.setPageIndex(1)
			.setOrder(order)
			.setPageSize(20)
			.postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		default:
			break;
		case R.id.carfix_distance:
			mAdapter.setDistance(true);
			order = "1";
			mPrice.setTextColor(getResources().getColor(R.color.green));
			mDistance.setTextColor(getResources().getColor(R.color.white));
			mDistance.setBackgroundResource(R.drawable.bg_tx_left_pressed);
			mPrice.setBackgroundResource(R.drawable.bg_tx_right);
			mReq.setLat(0.0)
				.setLon(0.0)
				.setPageIndex(1)
				.setOrder(order)
				.setPageSize(20)
				.postJsonRequest(mContext, mHandler);
			break;
		case R.id.carfix_price:
			mAdapter.setDistance(false);
			order = "2";
			mDistance.setTextColor(getResources().getColor(R.color.green));
			mPrice.setTextColor(getResources().getColor(R.color.white));
			mDistance.setBackgroundResource(R.drawable.bg_tx_left);
			mPrice.setBackgroundResource(R.drawable.bg_tx_right_pressed);
			mReq.setLat(0.0)
				.setLon(0.0)
				.setPageIndex(1)
				.setOrder(order)
				.setPageSize(20)
				.postJsonRequest(mContext, mHandler);
			break;
		}

	}

	@Override
	public void onResponse(IResponseHandler classz) {
		// insert db
		page = ((CarFixShopResponse)classz.getResponse()).datas;
		if(page.currentPageNo <= 1){
			mAdapter.setDatas(page.datas);
		}else{
			mAdapter.addDatas(page.datas);
			mAdapter.notifyDataSetChanged();
		}
		mListView.onRefreshComplete();
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "加载失败", Toast.LENGTH_LONG).show();
		mListView.onRefreshComplete();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> arg0) {
		mReq.setLat(0.0)
			.setLon(0.0)
			.setPageIndex(1)
			.setOrder(order)
			.setPageSize(20)
			.postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> arg0) {
		if(page==null){
			mReq.setLat(0.0)
				.setOrder(order)
				.setLon(0.0)
				.setPageIndex(1)
				.setPageSize(20)
				.postJsonRequest(mContext, mHandler);
		}else if(page.currentPageNo >= page.lastPageNo){
			Toast.makeText(mContext, "没有了",Toast.LENGTH_LONG).show();
			mListView.onRefreshComplete();
		}else{
			mReq.setLat(0.0)
				.setOrder(order)
				.setLon(0.0)
				.setPageIndex(page.nextPage)
				.setPageSize(page.pageSize)
				.postJsonRequest(mContext, mHandler);
		}
	}

	@Override
	public void onLastItemVisible() {
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		i.putExtra("data", mAdapter.getItem(position-1));
		startActivity(i);
	}
}
